<?php
namespace app\common\model;

class Images extends Common
{
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit']))
        {
            $limit = $post['limit'];
        }
        else
        {
            $limit = config('paginate.list_rows');
        }
        $tableWhere = $this->tableWhere($post);
        $config['page'] = $post['page'];
        $config['list_rows'] = $post['limit'];
        $list = $this->field($tableWhere['field'])
            ->where($tableWhere['where'])
            ->order($tableWhere['order'])
            ->paginate($limit, false, $config);
        $data = $this->tableFormat($list->getCollection()); //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;
        $re['sql'] = $this->getLastSql();
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['seller_id']) && $post['seller_id'] != "")
        {
            $where[] = ['seller_id', 'eq', $post['seller_id']];
        }
        if(isset($post['name']) && $post['name'] != "")
        {
            $where[] = ['name', 'like', '%'.$post['name'].'%'];
        }
        if(isset($post['id']) && $post['id'] != "")
        {
            $where[] = ['id', 'eq', $post['id']];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['ctime' => 'desc'];
        return $result;
    }


    /**
     * 根据查询结果，格式化数据
     * @param $list
     * @return mixed
     */
    protected function tableFormat($list)
    {
        if(!$list->isEmpty())
        {
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
            foreach($list as $key=>$val)
            {
                $list[$key]['ctime'] = date('Y-m-d H:i:s', $val['ctime']);
            }
        }
        return $list;
    }
}